import { Component, OnInit,ViewChild } from '@angular/core';
import {ModalDirective} from 'ngx-bootstrap';
import { AddTemplateType,EditTemplateType } from '../template';
import {HttpService} from '../../../lib/service/http.service';
import {adSystemApiUrl} from '../../../../config/api';

@Component({
  selector: 'app-template-type',
  templateUrl: 'template-type.component.html',
  styleUrls: ['template-type.component.scss']
})
export class templateTypeComponent implements OnInit {

  totalItems: number;
  itemsPerPage = 12;
  currentPage = 1;
  addTemplateType = new AddTemplateType;
  editTemplateType = new EditTemplateType;
  type: string;
  message: string;
  isShow = false;
  dataList: any = '';  // 模板类型列表
  operateId: number;
  searchData: string;  // 搜索名称

  @ViewChild('addModal') public addModal: ModalDirective;
  @ViewChild('editModal') public editModal: ModalDirective;
  @ViewChild('delModal') public delModal: ModalDirective;

  constructor(private httpService: HttpService) { }

  ngOnInit() {
    this.getTemplateTypeList(1);
  }

  // 获取模板类型列表
  getTemplateTypeList(page: number,name?: string){
    if(!!name){
      var url = `${adSystemApiUrl}/v1/admin/template_category/list?page=${page}&pageSize=${this.itemsPerPage}&name=${name}`;
    }else{
      var url = `${adSystemApiUrl}/v1/admin/template_category/list?page=${page}&pageSize=${this.itemsPerPage}`;
    }
    return this.httpService.httpGetAuth(url)
      .subscribe(
        res => {
          this.totalItems = res.data.total;
          this.dataList = res.data.data;
        }
      )
  }

  // 添加模板类型提交表单
  addTypeSubmit(){
    const url = `${adSystemApiUrl}/v1/admin/template_category/add`;
    return this.httpService.httpPostAuth(url,this.addTemplateType)
      .subscribe(
        res => {
          if(res.code === 2000){
            this.type = 'success';
            this.message = '添加成功';
            this.isShow = true;
            this.addModal.hide();
            this.getTemplateTypeList(1);
          }
        }
      )
  }

  // 编辑模板类型
  editType(id: number,name: string,describe: string,remark: string){
    this.editTemplateType.id = id;
    this.editTemplateType.name = name;
    this.editTemplateType.describe = describe;
    this.editTemplateType.remark = remark;
    this.editModal.show();
  }

  // 编辑模板类型提交表单
  editTypeSubmit(){
    const url = ``;
    return this.httpService.httpPostAuth(url,this.editTemplateType)
      .subscribe(
        res => {
          if( res.code === 2000 ){
            this.type = 'success';
            this.message = '修改成功';
            this.isShow = true;
            this.editModal.hide();
            this.getTemplateTypeList(1);
          }else{
            this.type = 'error';
            this.message = res.message;
            this.isShow = true;
          }
        }
      )
  }

  // 删除弹框
  delType(id: number){
    this.operateId = id;
    this.delModal.show();
  }

  // 删除类型提交表单
  delTypeSubmit(){
    const url = `${adSystemApiUrl}/v1/admin/template_category/${this.operateId}/del`;
    return this.httpService.httpPostAuth(url,'')
      .subscribe(
        res => {
          if( res.code === 2000 ){
            this.delModal.hide();
            this.type = 'success';
            this.message = '删除成功';
            this.isShow = true;
            this.getTemplateTypeList(1);
          }else{
            this.type = 'error';
            this.message = res.message;
            this.isShow = true;
          }
        }
      )
  }

  // 搜索
  search(){
    this.getTemplateTypeList(1,this.searchData);
  }
  // 重置
  reset(){
    this.searchData = '';
    this.getTemplateTypeList(1);
  }

  pageChanged(event: any): void{
    window.scrollTo(0,0);
    this.getTemplateTypeList(event.page,this.searchData)
  }

  promptIsShow(event: any): void{
    this.isShow = event;
  }
}
